package cloud.maque.sso.server.service.impl;

import cloud.maque.auth.exceptions.UserInfoException;
import cloud.maque.auth.fegin.AuthUserFeginClient;
import cloud.maque.auth.model.BaseUserModel;
import cloud.maque.auth.model.LoginRequest;
import cloud.maque.auth.model.LoginResponse;
import cloud.maque.common.core.enums.LoginType;
import cloud.maque.common.core.exceptions.BusinessException;
import cloud.maque.common.core.response.R;
import cloud.maque.sso.server.conf.MockDisabledCondition;
import cloud.maque.sso.server.service.UserHandle;
import cn.dev33.satoken.stp.SaLoginConfig;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Service
@Conditional(MockDisabledCondition.class)
public class UserHandleImpl implements UserHandle {


    @Autowired
    AuthUserFeginClient authUserFeginClient;

    /**
     * 密码登录，默认通过Fegin调通Auth服务登录，可配置模拟直接返回
     *
     * @param name
     * @param pwd
     * @return
     */
    public SaResult login(String name, String pwd) {


        LoginRequest loginRequest = new LoginRequest();
        loginRequest.setUserName(name);
        loginRequest.setPassWord(pwd);
        loginRequest.setLoginType(LoginType.PASSWORD);

        R<LoginResponse> responseR = authUserFeginClient.Login(loginRequest);
        if (responseR.getCode() == 200) {
            StpUtil.login(responseR.getData().getUserId(), SaLoginConfig.setExtra("userName", name));
            return SaResult.ok();
        } else {
            return SaResult.error("账号名或密码错误");
        }
    }

    /**
     * 只检查用户密码是否正确，不登录
     *
     * @param name
     * @param pwd
     * @return
     */
    @Override
    public SaResult checkPass(String name, String pwd) {
        LoginRequest loginRequest = new LoginRequest();
        loginRequest.setUserName(name);
        loginRequest.setPassWord(pwd);
        loginRequest.setLoginType(LoginType.PASSWORD);

        R<LoginResponse> responseR = authUserFeginClient.Login(loginRequest);
        if (responseR.getCode() == 200) {
            return SaResult.ok().setData(responseR.getData());
        } else {
            return SaResult.error("账号名或密码错误");
        }
    }

    /**
     * 获取用户信息
     *
     * @param userId
     * @return
     */
    @Override
    public BaseUserModel getUserInfo(String userId) {
        R<BaseUserModel> responseR = authUserFeginClient.GetUserInfo(userId);
        if (responseR.getCode() == 200) {
            return responseR.getData();
        }
        throw new UserInfoException(responseR.getMsg());
    }

    /**
     * 获取用户TOTP密钥
     *
     * @param userId
     */
    @Override
    public String getUserTotpSecret(String userId) {
        R<String> responseR = authUserFeginClient.getUserTotpSecret(userId);
        if (responseR.getCode() == 200) {
            return responseR.getData();
        }
        throw new UserInfoException(responseR.getMsg());
    }

    @Override
    public void updateUserTotpSecret(String userId, String secret) {
        R<Boolean> responseR = authUserFeginClient.updateUserTotpSecret(userId, secret);
        if (responseR.getCode() != 200) {
            throw new BusinessException("更新失败：" + responseR.getMsg());
        }
    }
}
